package com.freak.wanandroid.base.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.freak.wanandroid.R;
import com.freak.wanandroid.app.App;
import com.freak.wanandroid.base.lifecycle.MyLifecycleObserver;
import com.freak.wanandroid.util.ToolUtils;

/**
 * activity基类
 *
 * @author freak
 * @date 2022/09/12.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private AppCompatActivity mCurrentActivity;

    /**
     * 资源文件绑定
     *
     * @param id 资源文件id
     * @return View
     */
    protected <T extends View> T getViewById(int id) {
        return (T) findViewById(id);
    }

    /**
     * 绑定布局
     *
     * @return
     */
    protected abstract int getLayout();

    /**
     * 初始化事件和数据
     */
    protected abstract void initEventAndData();

    /**
     * 资源释放
     */
    protected abstract void onDestroyRelease();

    /**
     * 控件初始化
     */
    protected abstract void initView();

    /**
     * 系统返回键
     *
     * @param item MenuItem
     * @return 返回
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            super.onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        //限制竖屏
        //8.0.0版本系统，同时设置竖屏和设置全屏透明冲突，
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
//        App.resetDensity(this, App.DESIGN_WIDTH);
        //活动控制器
        mCurrentActivity = this;
        App.getInstance().addActivity(this, this.getClass());
//        EventBus.getDefault().register(this);
        getLifecycle().addObserver(new MyLifecycleObserver());
        initView();
        initEventAndData();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onDestroyRelease();
        App.getInstance().removeActivity(this);
//        EventBus.getDefault().unregister(this);
    }

    /**
     * 打开一个Activity 默认 不关闭当前activity
     *
     * @param className
     */
    public void gotoActivity(Class<?> className) {
        gotoActivity(className, false);
    }

    /**
     * 打开一个Activity  关闭当前activity
     *
     * @param className
     */
    public void gotoActivityWithFinish(Class<?> className) {
        gotoActivity(className, true);
    }

    /**
     * 打开一个Activity，并控制是否finish
     *
     * @param className              className
     * @param isCloseCurrentActivity 是否关闭
     */
    public void gotoActivity(Class<?> className, boolean isCloseCurrentActivity) {
        gotoActivity(className, isCloseCurrentActivity, null);
    }

    /**
     * 打开一个activity，并传递数据
     *
     * @param className              className
     * @param isCloseCurrentActivity 是否关闭
     * @param bundle                 bundle数据
     */
    public void gotoActivity(Class<?> className, boolean isCloseCurrentActivity, Bundle bundle) {
        Intent intent = new Intent(this, className);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        if (isCloseCurrentActivity) {
            finish();
        }
    }

    /**
     * 打开一个带结果返回的activity，并传递数据
     *
     * @param className   className
     * @param bundle      bundle数据
     * @param requestCode 请求码
     */
    public void gotoActivityWithResult(Class<?> className, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, className);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    public void showLoading() {
        ToolUtils.showLoading(mCurrentActivity);
    }

    public void dismissLoading() {
        ToolUtils.dismissLoading(mCurrentActivity);
    }

    public void setTitleString(String titleString) {
        TextView title = getViewById(R.id.title);
        title.setText(titleString);
    }

    public void setBackPress() {
        View backView = findViewById(R.id.linearLayoutBack);
        backView.setOnClickListener(v -> finish());
    }
}
